<?php
namespace Home\Controller;
use Think\Controller;
class ClassController extends Controller{
public function classlist(){
      //调用模型层操作数据
           $classmodel=M('class');
           //var_dump($goodsmodel);
            $this->class=$classmodel->order('classid desc')->select();
      //调用视图层显示结果
           $this->display();
    }
      public function findclass(){

    $id = $_GET["id"];
    $table = D('class');   
    $data = $table->where("classid=".$id)->select();
    // $this->hello($data);
    // return $data;
    $this->ajaxReturn($data);
    
  }



public function add(){
    // var_dump($_FILES);exit;
    $class = D('class');
    $class->classname=$_POST['name'];
    $class->classcost=$_POST['classcost'];
    $class->address=$_POST['address'];
    $class->difficulty=$_POST['difficulty'];
    $class->time=$_POST['time'];
      if($_FILES["classpic"]["error"]==0){
    $info=$this->upload();
    // var_dump($info);exit;
      $class->classpic=$info["classpic"]["savename"];
  }
    else{
      $this->error("必须选择图片"); 
    }
  if (false !== $class->add()) {
                $this->success("注册成功!","classlist" );
            } else {         $this->error('数据错误');           }
          

     }
     public function upload(){
    $upload = new \Think\Upload();// 实例化上传类
    $upload->maxSize   =     3145728 ;// 设置附件上传大小
    $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
    $upload->rootPath  =     './Public/photo/class/'; // 设置附件上传根目录
    $upload->autoSub=false;//关闭子目录，默认ture
    //$upload->savePath  =     ''; // 设置附件上传（子）目录,默认目录名为日期
    $info   =   $upload->upload();  // 上传文件 
    if(!$info) { 
    $this->error($upload->getError());    }
    else{
    return $info;   }                    
  }
    public function update(){
    $table = D("class");
    $id = $_POST['cid'];
    $pic = $table->where("classid=".$id)->field("classpic")->find();
    $table->classname=$_POST['name'];
    $table->classcost=$_POST['classcost'];
    $table->address=$_POST['address'];
    $table->time=$_POST['time'];
    $table->difficulty=$_POST['difficulty'];
    $table->people=$_POST['people'];
    if($_FILES["pic"]["error"]==0){
          $info=$this->upload();
          $table->classpic=$info["pic"]["savename"];
          unlink(realpath('./Public/photo/class/'.$pic["classpic"]));
        }else{
          $table->classpic = $pic;
        }
        $result = $table->where("classid=".$id)->save();
        if($result != 0){
      $this->success("修改课程成功！",U('classlist'));//url
    }else{
      $this->error("修改课程出错！");
    }
    // $this->success("ff",U("Video/video"));
  }
      public function delete(){

        $id=I('get.classid');
        $classModel=M("class");
        $pic = $classModel->where("classid=".$id)->field("classpic")->find();
        $res=$classModel->delete($id);
        unlink(realpath("./Public/photo/class/".$pic["classpic"]));
        if($res!=false){$this->success("删除成功");}
        else{$this->error("删除失败");}
    }



 }?>